//
//  WQKeyboardToolBar.swift
//  sanwuWeibo
//
//  Created by 王先强 on 15/12/16.
//  Copyright © 2015年 王先强. All rights reserved.
//

import UIKit

protocol WQKeyboardToolBarDelegate: NSObjectProtocol {
    
    func keyboardToolBarButtonDidClick(toolBarButton: WQKeyboardButton)
}

enum WQKeyboardButtonType: String {
    case cameraButton = "cameraButton"
    case pictureButton = "pictureButton"
    case mentionButton = "mentionButton"
    case emotionButton = "emotionButton"
}

class WQKeyboardToolBar: UIView {
    
    weak var delegate: WQKeyboardToolBarDelegate?
    
    lazy var imageBtn: UIButton = UIButton()

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupBarBtn()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupBarBtn() {
       
        addSubview(camerabutton)
        addSubview(pictureButton)
        addSubview(mentionButton)
        addSubview(emotionButton)
        
        camerabutton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.bottom.equalTo(self.snp_bottom)
        }
        pictureButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(camerabutton.snp_right)
            make.bottom.equalTo(self.snp_bottom)
            make.width.equalTo(camerabutton.snp_width)
        }
        
        mentionButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(pictureButton.snp_right)
            make.bottom.equalTo(self.snp_bottom)
            make.width.equalTo(pictureButton.snp_width)
        }
        emotionButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(mentionButton.snp_right)
            make.bottom.equalTo(self.snp_bottom)
            make.width.equalTo(mentionButton.snp_width)
            make.right.equalTo(self.snp_right)
        }
        
        
        camerabutton.addTarget(self, action: "toolBarButtonClicked:", forControlEvents: .TouchUpInside)
        pictureButton.addTarget(self, action: "toolBarButtonClicked:", forControlEvents: .TouchUpInside)
        mentionButton.addTarget(self, action: "toolBarButtonClicked:", forControlEvents: .TouchUpInside)
        emotionButton.addTarget(self, action: "toolBarButtonClicked:", forControlEvents: .TouchUpInside)
    }


    
    private lazy var camerabutton: WQKeyboardButton = WQKeyboardButton(imageName: "compose_camerabutton_background", barButtonType: WQKeyboardButtonType.cameraButton)
    private lazy var pictureButton: WQKeyboardButton = WQKeyboardButton(imageName: "compose_toolbar_picture", barButtonType: WQKeyboardButtonType.pictureButton)
    private lazy var mentionButton: WQKeyboardButton = WQKeyboardButton(imageName: "compose_mentionbutton_background", barButtonType: WQKeyboardButtonType.mentionButton)
    private lazy var emotionButton: WQKeyboardButton = WQKeyboardButton(imageName: "compose_emoticonbutton_background", barButtonType: WQKeyboardButtonType.emotionButton)
    
    private var isEmotionKeyborad = true
    @objc private func toolBarButtonClicked(button: WQKeyboardButton) {
        
        if button.toolBarButtonType == WQKeyboardButtonType.emotionButton {
            
            if isEmotionKeyborad {
                emotionButton.changeButtonImage("compose_keyboardbutton_background")
                isEmotionKeyborad = false
            }else{
                emotionButton.changeButtonImage("compose_emoticonbutton_background")
                isEmotionKeyborad = true
            }
        }
        
        delegate?.keyboardToolBarButtonDidClick(button)
    }
}



class WQKeyboardButton: UIButton {
    var toolBarButtonType: WQKeyboardButtonType
    init(type: WQKeyboardButtonType) {
        self.toolBarButtonType = type
        super.init(frame: CGRectZero)
        
    }
    convenience init(imageName: String, barButtonType: WQKeyboardButtonType) {
        self.init(type: barButtonType)
        setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        setImage(UIImage(named: imageName + "_highlighted"), forState: UIControlState.Highlighted)
        backgroundColor = randomColor

    }

    required init?(coder aDecoder: NSCoder) {

        fatalError("init(coder:) has not been implemented")
    }
}
